<template>
  <d2-container>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="data"
      :loading="loading"
      :pagination="pagination"
      :rowHandle="rowHandle"
      :add-template="template"
      :edit-template="template"
      :form-options="formOptions"
      :options="options"
      @row-add="handleRowAdd"
      @row-edit="handleRowEdit"
      @row-remove="handleRowRemove"
      @dialog-cancel="handleDialogCancel"
      @pagination-current-change="paginationCurrentChange">
      <el-button slot="header" style="margin: 7px" @click="addRow">新增</el-button>
    </d2-crud>
  </d2-container>
</template>    

<script>
export default {
  data () {
    return {
      columns: [
        {
    title: 'ID',
    key: 'id'
},    
{
    title: '类别描述',
    key: 'description'
},    
{
    title: '类别名称',
    key: 'name'
},    
      ],
      data: [],
      rowHandle: {
        columnHeader: '操作',
        fixed: 'right',
        width: '200',
        edit: {
          icon: 'el-icon-edit',
          text: '编辑',
          size: 'small'
        },
        remove: {
          icon: 'el-icon-delete',
          size: 'small',
          fixed: 'right',
          confirm: true
        }
      },
      template: {
        id: {
          title: 'ID',
          value: ''
        },
description: {
          title: '类别描述',
          value: ''
        },
name: {
          title: '类别名称',
          value: ''
        },
      },
      formOptions: {
        labelWidth: '120',
        labelPosition: 'left',
        saveLoading: false
      },
      options: {
        stripe: true
      },
      loading: false,
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  mounted () {
    this.fetchData()
  },
  methods: {
    paginationCurrentChange (currentPage) {
      this.pagination.currentPage = currentPage
      this.fetchData()
    },
    fetchData () {
      this.loading = true
      this.$api.modules['pms-category'].PAGE({
        ...this.pagination
      }).then(res => {
        this.data = res.data.list
        this.pagination.total = res.data.total
        this.loading = false
      }).catch(err => {
        console.log('err', err)
        this.loading = false
      })
      this.loading = false
    },
    addRow () {
      this.$refs.d2Crud.showDialog({
        mode: 'add'
      })
    },
    handleRowAdd (row, done) {
      this.formOptions.saveLoading = true
      setTimeout(() => {
        this.$api.modules['pms-category'].ADD(row).then((response) => {
          this.open = false
          this.title = ''
          done({
            id: response.data.id
          })
          this.$message({
            message: '保存成功',
            type: 'success'
          })
        })

        this.formOptions.saveLoading = false
      }, 100)
    },
    handleRowEdit ({
      index,
      row
    }, done) {
      this.formOptions.saveLoading = true
      setTimeout(() => {
        this.$api.modules['pms-category'].UPDATE(row).then((response) => {
          this.open = false
          this.title = ''
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
        })

        // done可以传入一个对象来修改提交的某个字段
        done({
        })
        this.formOptions.saveLoading = false
      }, 100)
    },
    handleRowRemove ({
      index,
      row
    }, done) {
      setTimeout(() => {
        this.$api.modules['pms-category'].DELETE(row.id)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        done()
      }, 100)
    },
    handleDialogCancel (done) {
      this.$message({
        message: '取消操作',
        type: 'warning'
      })
      done()
    }
  }
}
</script>

